define(function (require, exports, module) {

/**
 * 表单提交
 *
 * @module Form
 */

'use strict';

var Widget = require('widget');

/**
 * Core
 *
 * @class Core
 * @constructor
 */
module.exports = Widget.extend({

  defaults: {
    classPrefix: 'ue-form',
    data: {
      layout: 'horizontal',
      groups: [ ]
    },
    // 表单项值
    // values: {},
    template: require('./form.handlebars'),
    templateOptions: {
      helpers: {
        // ifequal: function(v1, v2, options) {
        //   return (v1 == v2) ? options.fn(this) : options.inverse(this);
        // },
        isInTypes: function(type, types, options) {
          return (types.split(',').indexOf(type) !== -1) ? options.fn(this) : options.inverse(this);
        },
        getColSpan: function(span, options) {
          if (span === null) {
            return '';
          }
          (typeof span === 'undefined') && (span = 3);
          return 'col-sm-' + (span--) + ' col-md-' + (span--) + ' col-lg-' + (span);
        },
        getColSpanRest: function(span, options) {
          if (span === null) {
            return '';
          }
          (typeof span === 'undefined') && (span = 3);
          span = 12 - span;
          return 'col-sm-' + (span++) + ' col-md-' + (span++) + ' col-lg-' + (span);
        },
        getColSpanOffset: function(span, options) {
          if (span === null) {
            return '';
          }
          (typeof span === 'undefined') && (span = 3);
          return 'col-sm-offset-' + (span) + ' col-sm-' + (12 - span--) +
            ' col-md-offset-' + (span) + ' col-md-' + (12 - span--) +
            ' col-lg-offset-' + (span) + ' col-lg-' + (12 - span);
        },
        showCol: function(span, options) {
          return span === null  ? options.inverse(this) : options.fn(this);
        }
      },
      partials: {
        groups: require('./form-groups.handlebars'),
        types: require('./form-types.handlebars'),
        notype: require('./form-notype.handlebars'),
        element: require('./form-element.handlebars'),
        attrs: require('./form-attrs.handlebars'),
        buttons: require('./form-buttons.handlebars')
      }
    }
  },

  setup: function () {
    this.initValues();
    this.trans();
    this.render();
  },

  initValues: function () {
    var self = this,
        groups,
        values = this.option('values');

    // 遍历 select/options
    function walkOptions(options, value) {
      options.forEach(function (option) {
        option.selected = (value === option.value);
      });
    }

    // 遍历 data/groups
    function walkGroups(groups) {
      groups.forEach(function (item) {
        var attrs,
            options,
            key;

        if (item.groups) {
          walkGroups(item.groups);
          return true;
        }

        attrs = item.attrs;

        if (!attrs || !attrs.name) {
          return true;
        }

        options = item.options;

        for (key in values) {

          if (attrs.name === key) {

            if (options) {
              walkOptions(options, values[key]);
              break;
            }

            if (/^(checkbox|radio)$/.test(attrs.type)) {
              attrs.checked = (values[key] === attrs.value);
              break;
            }

            attrs.value = values[key];
            break;
          }
        }
      });
    }

    if (!values) {
      return;
    }

    groups = self.data('groups');

    if (!groups || !groups.length) {
      return;
    }

    walkGroups(groups);
  },

  // 为了兼容 CHROME 36
  trans: function () {
    function tranAttrs (attrs) {
      var arr = [],
          key;
      for (key in attrs) {
        arr.push({
          name: key,
          value: attrs[key]
        });
      }
      return arr;
    }

    function transGroups (groups) {
      if (!groups) {
        return;
      }

      groups.forEach(function (group) {
        if (group.attrs) {
          group.attrs2 = tranAttrs(group.attrs);
        }

        transGroups(group.groups);
      });
    }

    transGroups(this.data('groups'));
    transGroups(this.data('buttons'));
  }

});

});
